package com.urbanski.util;

public class Utils
{
	/**
	 * Determines whether an object is in a set of objects.  Behaves analogous to the SQL
	 * where in clause.
	 * 
	 * @param object the object to check
	 * @param list the objects to search
	 * @return true of object is in the list of objects, or false otherwise.
	 */
	public static boolean in(Object object, Object... list)
	{
		for (Object o : list)
		{
			if (object.equals(o))
			{
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Inserts the specified string into the filename before the extension.  The file extension
	 * is defined as all the text following the last period, including the period itself.  If there is no file
	 * extension, then simply concatenates to the end of the filename.
	 * 
	 * For example, if filename = "file1.txt" and stringToInsert = "_append",
	 * then this method returns "file1_append.txt".
	 * 
	 * @param filename the filename into which to insert the string
	 * @param stringToInsert the string to insert into the filename
	 * @return a new filename with stringToInsert inserted before the file extension
	 */
	public static String insertInFilename(String filename, String stringToInsert)
	{
		int extensionIndex = filename.lastIndexOf('.');
		if (extensionIndex == -1)
		{
			return filename + stringToInsert;
		}
		else
		{
			return filename.substring(0, extensionIndex) + stringToInsert + filename.substring(extensionIndex);
		}
	}
	
	public static int parseInt(String s, int defaultValue)
	{
		return parseInt(s, defaultValue, false);
	}
	public static int parseInt(String s, int defaultValue, boolean trim)
	{
		try
		{
			defaultValue = Integer.parseInt(trim ? s : s.trim());
		}
		catch (NumberFormatException e)
		{
			
		}
		return defaultValue;
	}
}
